package com.one.component;

import com.one.domain.ResultInfo;
import com.one.listener.ActionDownListener;
import com.one.net.GetUtils;
import com.one.utils.JsonUtils;
import com.one.utils.ResultInfoData2Vector;

import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Vector;

public class BookManageComponent extends Box {
    final int WIDTH = 850;
    final int HEIGHT = 80;
    JFrame jf = null;
    private JTable table;
    private Vector<String> titles;
    private Vector<Vector> tableData;
    private DefaultTableModel tableModel;

    public BookManageComponent(JFrame jf) {
        // 垂直布局
        super(BoxLayout.Y_AXIS);
        this.jf = jf;
        // 组装视图
        JPanel btnPanel = new JPanel();
        Color color = new Color(203, 220, 217);
        btnPanel.setBackground(color);
        btnPanel.setMaximumSize(new Dimension(WIDTH,HEIGHT));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton addBtn =new JButton("添加");
        JButton upBtn =new JButton("修改");
        JButton delBtn =new JButton("删除");

        addBtn.addActionListener(e -> {
            // 弹出一个对话框,让用户输入图书信息
            // 重新调用requestData方法,获取最新数据
            new AddBookDialog(jf, "添加图书", true, result -> {
                requestData();
            }).setVisible(true);
        });

        upBtn.addActionListener(e -> {
            // 获取当前表格选中的id
            // 条目存在返回选中行号,如果没选中,返回-1
            int selectedRow = table.getSelectedRow();
            if(selectedRow == -1) {
                JOptionPane.showMessageDialog(jf,"请选择要修改的条目!");
                return;
            }
            String id = tableModel.getValueAt(selectedRow,0).toString();
            // 弹出一个对话框,用户修改并刷新
            new UpdateBookDialog(jf,"修改图书",true,result -> {
                requestData();
            },id).setVisible(true);
        });

        delBtn.addActionListener(e -> {
            // 获取选中条目
            int selectedRow = table.getSelectedRow();
            if(selectedRow == -1) {
                JOptionPane.showMessageDialog(jf,"请选择要删除的条目!");
                return;
            }
            // 防止误触操作
            int res = JOptionPane.showConfirmDialog(jf, "确认要删除选中的条目吗?", "确认删除", JOptionPane.YES_NO_OPTION);
            if (res != JOptionPane.YES_OPTION) {
                return;
            }
            String id = tableModel.getValueAt(selectedRow,0).toString();
            GetUtils.get("http://localhost:8080/book/deleteBookById?id="+id,result -> {
                ResultInfo info = JsonUtils.parseResult(result);
                if (info.isFlag()) {
                    JOptionPane.showMessageDialog(jf,"删除成功");
                    requestData();
                } else {
                    JOptionPane.showMessageDialog(jf,info.getMessage());
                }
            },() -> {
                JOptionPane.showMessageDialog(jf,"网络异常,请稍后重试");
            });
        });
        btnPanel.add(addBtn);
        btnPanel.add(upBtn);
        btnPanel.add(delBtn);
        this.add(btnPanel);

        // 组装表格
        String[] ts = {"编号","书名","简介","作者","价格","库存"};
        titles = new Vector<>();
        for (String title : ts) {
            titles.add(title);
        }
        tableData = new Vector<>();
        tableModel = new DefaultTableModel(tableData,titles);
        table = new JTable(tableModel) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        // 设置只能选中一行
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane =new JScrollPane(table);
        this.add(scrollPane);
        requestData();
    }
    // 请求数据
    public void requestData() {
        GetUtils.get("http://localhost:8080/book/findAllBook",result -> {
            ResultInfo info = JsonUtils.parseResult(result);
            Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);
            // 清空tableData的数据
            tableData.clear();
            for (Vector vector : vectors) {
                tableData.add(vector);
            }
            // 刷新表格
            tableModel.fireTableDataChanged();
        },() -> {

        });
    }
}
